package com.xiaochong.tir.manager.filter;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.Account;
import com.xiaochong.tir.common.util.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xufanglin
 * @date 2017-12-20 19:29 Created by xufanglin on 2017/12/20 0020.
 *       登陆验证使用过滤器(暂不考虑使用拦截器)
 */
@Configuration
public class LoginFilter extends OncePerRequestFilter {

	/**
	 * 验证登陆前缀
	 */
	private final String AUTH_PREFIX = "/manage/";

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String uri = request.getRequestURI();
		/*LogTrace.info(getClass().getSimpleName(), "uri = " + uri);*/
		if (uri != null && uri.indexOf(AUTH_PREFIX) != -1) {
			Account account = SessionManager.getLoginAccount(request.getSession());
			if (account == null) {
				/*LogTrace.info(getClass().getSimpleName(), "no login account");*/
				response.sendRedirect(Constants.LOGIN);
				return;
			}
		}
		filterChain.doFilter(request, response);
	}

}
